<?php
namespace Member\Controller;
use Think\Controller;
class orderController extends CommonController {
    public function lists(){

        $map['member_id'] = $_SESSION['member']['id'];

        $list = D('Admin/order')->where($map)->select();

        foreach($list as $key =>$v){
            $list[$key]['goods_desc'] = json_decode($v['goods_desc'],true);
        }
// print_r($list);die;
        $this->assign('list',$list);
        $this->assign('order_status',C('ORDER_STATUS'));// 订单状态
        $this->display();
    }

    /**
     * 订单详情
     *
     * @return void
     * @author
     **/
    function info()
    {
        // print_r($_SESSION);die;
        $this->display();
    }

    /**
     * 订单支付
     *
     * @return void
     * @author
     **/
    function pay()
    {
        // 查询购物车里的数据
        $map['member_id'] = $_SESSION['member']['id'];
        $map['endble'] = 0;// 查询当前用户未生成订单的购物车商品
        $cart = D('Admin/cart')->where($map)->relation(true)->select();

        // 组装订单数据
        $data['order_no'] = date('Ymdhis',time()).mt_rand(10000,99999);// 订单编号
        $data['member_id'] = $_SESSION['member']['id'];// 订单所属人id

        $data['total_price'] = 0;// 订单总价
        // 循环购物车计算订单总价
        foreach($cart as $key =>$v){
            // 组装需要显示的购物车商品信息
            $data['goods_desc'][$key]['goods_name'] = $v['goods']['goods_name'];// 商品名
            $data['goods_desc'][$key]['shop_price'] = $v['goods']['shop_price']+$v['spec_price']['spec_price'];// 商品单价
            $data['goods_desc'][$key]['spec_desc'] = $v['spec_price']['spec_desc'];// 商品规格
            $data['goods_desc'][$key]['img'] = $v['goods']['img'];// 商品图片
            $data['goods_desc'][$key]['goods_number'] = $v['goods_number'];// 商品购买数

            // 商品小计公式 = 数量*（单价+规格价格）
            $data['total_price'] += $v['goods_number']*($v['goods']['shop_price']+$v['spec_price']['spec_price']);

        }

        // 将商品信息转换成字符串格式存入数据库
        $data['goods_desc'] = json_encode($data['goods_desc']);

        // 实现订单添加
        $id = D('order')->add($data);
        if($id){
            // $field['endble'] = 1;// 逻辑删除下单成功 则购物车的商品就不应存在
            // D('Admin/cart')->where($map)->data($field)->save();

            //支付交易请求
            D('Home/pay_record')->buildRequestForm($data['order_no'],$data['total_price'],2,$_SESSION['member']['id'],$data['order_no']);
            // $this->success('下单成功',U('member/order/lists'),1);die;
        }else{
            // $this->error('下单失败','',1);die;
        }

        $this->display();
    }
}